<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">迁移数据记录列表</h3>
            </div>
            <div class="card-body">
                <h4>
                    <a target="_blank" href="${request.contextPath}/data/migrate/init" class="btn btn-info btn-success"
                       role="button">+ 添加新迁移</a>
                </h4>

                <form class="row mb-2 align-items-center" role="form" method="post" action="${request.contextPath}/data/migrate/index" id="pageForm" name="ec">
                    <div class="col-md-2">
                        <input type="text" class="form-control" id="sourceAppId" name="sourceAppId"
                               value="${appDataMigrateSearch.sourceAppId!}" placeholder="源appId">
                    </div>
                    <div class="col-md-2">
                        <input type="text" class="form-control" id="targetAppId" name="targetAppId"
                               value="${appDataMigrateSearch.targetAppId!}" placeholder="目标appId">
                    </div>
                    <div class="col-md-2">
                        <input type="text" class="form-control" id="migrateMachine" name="migrateMachine"
                               value="${appDataMigrateSearch.migrateMachine!}" placeholder="迁移机器">
                    </div>
                    <div class="col-md-2">
                        <select name="userId" class="form-select">
                            <option value="" <#if !(appDataMigrateSearch??) || (appDataMigrateSearch?? && (appDataMigrateSearch.userId! == ''))>selected</#if>>
                            操作人
                            </option>
                            <#list adminList as admin>
                                <option value="${admin.id!}" <#if appDataMigrateSearch?? && (appDataMigrateSearch.userId! == (admin.id?string))>selected</#if>>
                                【${admin.id!}】${admin.name!}&nbsp;${admin.chName!}
                                </option>
                            </#list>
                        </select>
                    </div>

                    <div class="col-md-2">
                        <select name="status" class="form-select">
                            <option value="3" <#if (appDataMigrateSearch.status == 3)>selected</#if>>
                            增量同步
                            </option>
                            <option value="-2" <#if (appDataMigrateSearch.status == -2)>selected</#if>>
                            全部状态
                            </option>
                            <option value="0" <#if (appDataMigrateSearch.status == 0)>selected</#if>>
                            全量同步
                            </option>
                            <option value="1" <#if (appDataMigrateSearch.status == 1)>selected</#if>>
                            同步结束
                            </option>
                            <option value="2" <#if (appDataMigrateSearch.status == 2)>selected</#if>>
                            同步异常
                            </option>
                        </select>
                    </div>

                    <input type="hidden" name="pageNo" id="pageNo">
                    <button type="submit" class="btn btn-primary col-md-1">查询</button>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="card-body table-responsive">
                <table class="table table-striped table-bordered table-hover table-sm">
                    <thead>
                    <tr>
                        <th scope="col">任务ID</th>
                        <th scope="col">迁移ID</th>
<!--                        <th scope="col">迁移工具</th>-->
                        <th scope="col">迁移机器</th>
                        <th scope="col">操作人</th>
                        <th scope="col">源数据</th>
                        <th scope="col">目标数据</th>
                        <th scope="col">开始/结束时间</th>
                        <th scope="col">状态</th>
                        <th scope="col">查看</th>
                        <th scope="col">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                        <#list appDataMigrateStatusList as appDataMigrateStatus>
                            <tr>
                                <td>
                                    ${appDataMigrateStatus.id!}
                                    <input type="hidden" id="id" value="${appDataMigrateStatus.id!}"/>
                                </td>
                                <td>
                                    ${appDataMigrateStatus.migrateId!}<br/>
                                    <#if appDataMigrateStatus.configPath??>
                                        <span class="btn btn-sm btn-secondary">${appDataMigrateStatus.configPath?split("/")[3]}</b>
                                    </#if>
                                    <input type="hidden" id="migrateTool" value="${appDataMigrateStatus.migrateTool!}"/>
                                </td>
<!--                                <td>-->
<!--                                    <#if (appDataMigrateStatus.migrateTool == 0)>-->
<!--                                        redis-shake-->
<!--                                    <#else>-->
<!--                                        redis-migrate-tool-->
<!--                                    </#if>-->
<!--                                </td>-->
                                <td>
                                    <#if (appDataMigrateStatus.migrateTool == 1)>
                                        ${appDataMigrateStatus.migrateMachineIp!}:${appDataMigrateStatus.migrateMachinePort!}
                                    <#else>
                                        ${appDataMigrateStatus.migrateMachineIp!}
                                    </#if>
                                </td>
                                <td>${appDataMigrateStatus.userName!}</td>
                                <td>
                                    数据源：
                                    <#if (appDataMigrateStatus.sourceAppId <= 0)>
                                        非cachecloud
                                        <br/>
                                        ${appDataMigrateStatus.sourceServers!}
                                    <#else>
                                        cachecloud:<a target="_blank" href="${request.contextPath}/admin/app/index?appId=${appDataMigrateStatus.sourceAppId!}">${appDataMigrateStatus.sourceAppId!}</a>
                                    </#if>
                                    <br/>
                                    源类型：redis-${appDataMigrateStatus.sourceMigrateTypeDesc!}
                                    <br/>
                                    redis版本：${appDataMigrateStatus.redisSourceVersion!}<br/>
                                    <#if (appDataMigrateStatus.sourceMigrateType == 8 || appDataMigrateStatus.sourceMigrateType == 9)>
                                        <a href="/data/migrate/tasklist?migrate_id=${appDataMigrateStatus.migrateId!}" class="btn btn-sm btn-warning">查看任务:${appDataMigrateStatus.taskCount!}</a>
                                    </#if>
                                </td>
                                <td>
                                    数据源：
                                    <#if (appDataMigrateStatus.targetAppId <= 0)>
                                        非cachecloud
                                        <br/>
                                        ${appDataMigrateStatus.targetServers!}
                                    <#else>
                                        cachecloud:<a target="_blank" href="${request.contextPath}/admin/app/index?appId=${appDataMigrateStatus.targetAppId!}">${appDataMigrateStatus.targetAppId!}</a>
                                    </#if>
                                    <br/>

                                    <#if (appDataMigrateStatus.sourceMigrateType == 8 || appDataMigrateStatus.sourceMigrateType == 9)>
                                    <#else>
                                         目标类型：redis-${appDataMigrateStatus.targetMigrateTypeDesc!}
                                    </#if>
                                    <br/>
                                    redis版本：${appDataMigrateStatus.redisTargetVersion!}
                                </td>
                                <td>
                                    ${appDataMigrateStatus.startTimeFormat!}<br/>
                                    ${appDataMigrateStatus.endTimeFormat!}
                                </td>
                                <td>${appDataMigrateStatus.statusDesc!}</td>
                                <td>
                                    <a target="_blank" href="${request.contextPath}/data/migrate/log?id=${appDataMigrateStatus.id!}">日志</a><br/><br/>
                                    <a target="_blank"
                                       href="${request.contextPath}/data/migrate/config?id=${appDataMigrateStatus.id!}">配置</a><br/><br/>
                                    <#if (appDataMigrateStatus.status == 0)>
                                        <#if (appDataMigrateStatus.migrateTool == 1)>
                                            <a target="_blank"
                                               href="${request.contextPath}/data/migrate/process?id=${appDataMigrateStatus.id!}&migrateTool=${appDataMigrateStatus.migrateTool!}">进度</a>
                                        </#if>
                                        <#if (appDataMigrateStatus.migrateTool == 0)>
                                            <a data-bs-toggle="modal" data-target="#processModal">进度</a>
                                        </#if>
                                    </#if>
                                    <div class="modal fade" id="processModal" tabindex="-1" role="dialog"
                                         aria-labelledby="myModalLabel" aria-hidden="true">
                                        <div class="modal-dialog">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h4 class="modal-title" id="myModalLabel">
                                                        迁移进度
                                                    </h4>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-hidden="true"></button>
                                                </div>
                                                <div class="modal-body" id="processModal_body">

                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭
                                                    </button>
                                                </div>
                                            </div><!-- /.modal-content -->
                                        </div><!-- /.modal -->
                                    </div>
                                </td>
                                <td>
                                    <button onclick="resyncMigrate(${appDataMigrateStatus.migrateId!})" type="button"
                                    class="btn btn-sm btn-warning">重新同步
                                    </button>
                                    <br/>
                                    <button  onclick="stopMigrate(${appDataMigrateStatus.id!},${appDataMigrateStatus.migrateId!})" type="button"
                                         <#if (appDataMigrateStatus.status == 1 || appDataMigrateStatus.status == 2)>
                                                disabled="disabled" class="btn btn-sm btn-secondary"
                                        <#else>
                                                class="btn btn-sm btn-danger"
                                        </#if>
                                    >停止任务
                                    </button>
                                    <br/>
                                    <a target="_blank" href="${request.contextPath}/data/migrate/checkDataRandom?id=${appDataMigrateStatus.id!}"
                                       class="btn btn-sm btn-info" role="button">数据校验</a>
                                </td>
                            </tr>
                        </#list>
                    </tbody>
                </table>
                <div style="margin-bottom: 10px;">
                    <span>
                        <div id="pageDetail"
                             style="float:left;padding-top:7px;color:#4A64A4;">
                            <#if page??>
                                共${page.totalPages!}页,${page.totalCount!}条
                            </#if>
                        </div>
                        <nav id='ccPagenitor' aria-label="Page navigation example" class="d-inline-flex float-end">
                        </nav>
                    </span>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $('#processModal').on('shown.bs.modal',
            function () {
                $.get(
                    '${request.contextPath}/data/migrate/process.json',
                    {
                        id: document.getElementById("id").value,
                        migrateTool: document.getElementById("migrateTool").value
                    },
                    function (data) {
                        var result = data.process.replace(/\n/, '<br>');
                        $("#processModal_body").html(result);
                    }
                );
            })
    });
    function stopMigrate(id,migrate_id) {
        if (window.confirm("确认要停掉id=" + id + "的迁移任务吗?")) {
            $.get(
                '${request.contextPath}/data/migrate/stop.json',
                {
                    id: id,
                    migrate_id: migrate_id
                },
                function (data) {
                    var status = data.status;
                    alert(data.message);
                    location.href = "${request.contextPath}/data/migrate/index";
                }
            );
        }
    }

    function resyncMigrate(migrate_id) {
        if (window.confirm("确认重新同步迁移id=" + migrate_id + "的迁移任务吗?")) {
            $.get(
                '${request.contextPath}/data/migrate/resync.json',
                {
                    migrate_id: migrate_id
                },
                function (data) {
                    var status = data.status;
                    alert(data.message);
                    location.href = "${request.contextPath}/data/migrate/index";
                }
            );
        }
    }


</script>
<#include "/manage/inc/page.html">
